<script setup lang="ts" name="ChatWindow">
import Portrait from '@/components/Portrait/index.vue';
import { Friend } from '@/types/index';
import { getChatMsg } from '@/api/data';
import { onMounted, ref } from 'vue';
import Emoji from '@/components/Emoji/index.vue';

interface IProps {
  friendInfo: Friend;
}

const props = defineProps<IProps>();

const chatList = ref<any>([]);
const srcImgList = ref<string[]>([]);
const showEmoji = ref(false);

const getFirendChatMsg = async () => {
  const res: any = await getChatMsg({ friendId: props.friendInfo.id });
  chatList.value = res;
  chatList.value.forEach((item: any) => {
    if (item.chatType == 2 && item.extend.imgType == 2) {
      srcImgList.value.push(item.msg);
    }
  });
};

onMounted(() => {
  getFirendChatMsg();
});
</script>

<template>
  <div class="chatwindow-page">
    <div class="chat-top">
      <div class="head-pic">
        <Portrait :imgUrl="props.friendInfo.headImg"></Portrait>
      </div>
      <div class="info-detail">
        <div class="name">{{ props.friendInfo.name }}</div>
        <div class="detail">{{ props.friendInfo.detail }}</div>
      </div>
      <div class="other-fun">
        <span class="iconfont icon-shipin"></span>
        <span class="iconfont icon-gf-telephone"></span>
        <label for="docFile">
          <span class="iconfont icon-wenjian"></span>
        </label>
        <label for="imgFile">
          <span class="iconfont icon-tupian"></span>
        </label>
        <input type="file" name="" id="imgFile" accept="image/*" />
        <input type="file" name="" id="docFile" accept="application/*,text/*" />
      </div>
    </div>

    <div class="chat-bottom">
      <div class="chat-content">
        <div class="chat-wrapper" v-for="item in chatList" :key="item.id">
          <div class="chat-friend" v-if="item.uid !== '1001'">
            <div class="chat-text" v-if="item.chatType == 0">
              {{ item.msg }}
            </div>
            <div class="chat-img" v-if="item.chatType == 1">
              <img
                :src="item.msg"
                alt="表情"
                v-if="item.extend.imgType == 1"
                style="width: 100px; height: 100px"
              />
              <el-image :src="item.msg" :preview-src-list="srcImgList" v-else></el-image>
            </div>
            <div class="chat-img" v-if="item.chatType == 2">
              <div class="word-file">
                <!-- <FileCard :fileType="item.extend.fileType" :file="item.msg"></FileCard> -->
              </div>
            </div>
            <div class="info-time">
              <img :src="item.headImg" alt="" />
              <span>{{ item.name }}</span>
              <span>{{ item.time }}</span>
            </div>
          </div>
          <div class="chat-me" v-else>
            <div class="chat-text" v-if="item.chatType == 0">
              {{ item.msg }}
            </div>
            <div class="chat-img" v-if="item.chatType == 1">
              <img
                :src="item.msg"
                alt="表情"
                v-if="item.extend.imgType == 1"
                style="width: 100px; height: 100px"
              />
              <el-image
                style="max-width: 300px; border-radius: 10px"
                :src="item.msg"
                :preview-src-list="srcImgList"
                v-else
              ></el-image>
            </div>
            <div class="chat-img" v-if="item.chatType == 2">
              <div class="word-file">
                <!-- <FileCard :fileType="item.extend.fileType" :file="item.msg"></FileCard> -->
              </div>
            </div>
            <div class="info-time">
              <span>{{ item.name }}</span>
              <span>{{ item.time }}</span>
              <img :src="item.headImg" alt="" />
            </div>
          </div>
        </div>
      </div>
      <div class="chatInputs">
        <div class="emoji boxinput">
          <img src="@/assets/img/emoji/smiling-face.png" alt="" @click="showEmoji = true" />
        </div>
        <div class="emoji-content">
          <Emoji v-model:show="showEmoji" v-show="showEmoji"></Emoji>
        </div>
        <input class="inputs" />
        <div class="send boxinput">
          <img src="@/assets/img/emoji/rocket.png" alt="" />
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.chatwindow-page {
  width: 100%;
  height: 100%;
  margin-left: 16px;
  position: relative;

  display: flex;
  flex-direction: column;

  .chat-top {
    margin-bottom: 48px;
    display: flex;

    .info-detail {
      margin-left: 16px;
      color: #fff;

      .name {
        font-size: 18px;
      }

      .detail {
        margin-top: 6px;
        font-size: 12px;
        color: #5c6675;
      }
    }

    .other-fun {
      flex: 1;
      display: flex;
      justify-content: end;
      align-items: center;
      column-gap: 32px;

      span {
        cursor: pointer;
      }

      input {
        display: none;
      }
    }
  }

  .chat-bottom {
    margin-bottom: 32px;
    flex: 1;
    background-color: rgb(50, 54, 68);
    border-radius: 20px;
    padding: 16px;
    position: relative;
    overflow-y: auto;

    .chat-content {
      width: 100%;
      height: 85%;
      overflow-y: scroll;
      padding: 20px;
      box-sizing: border-box;
      &::-webkit-scrollbar {
        width: 0; /* Safari,Chrome 隐藏滚动条 */
        height: 0; /* Safari,Chrome 隐藏滚动条 */
        display: none; /* 移动端、pad 上Safari，Chrome，隐藏滚动条 */
      }
      .chat-wrapper {
        position: relative;
        word-break: break-all;
        .chat-friend {
          width: 100%;
          float: left;
          margin-bottom: 20px;
          display: flex;
          flex-direction: column;
          justify-content: flex-start;
          align-items: flex-start;
          .chat-text {
            max-width: 90%;
            padding: 20px;
            border-radius: 20px 20px 20px 5px;
            background-color: rgb(56, 60, 75);
            color: #fff;
            &:hover {
              background-color: rgb(39, 42, 55);
            }
          }
          .chat-img {
            img {
              width: 100px;
              height: 100px;
            }
          }
          .info-time {
            margin: 10px 0;
            color: #fff;
            font-size: 14px;
            img {
              width: 30px;
              height: 30px;
              border-radius: 50%;
              vertical-align: middle;
              margin-right: 10px;
            }
            span:last-child {
              color: rgb(101, 104, 115);
              margin-left: 10px;
              vertical-align: middle;
            }
          }
        }
        .chat-me {
          width: 100%;
          float: right;
          margin-bottom: 20px;
          position: relative;
          display: flex;
          flex-direction: column;
          justify-content: flex-end;
          align-items: flex-end;
          .chat-text {
            float: right;
            max-width: 90%;
            padding: 20px;
            border-radius: 20px 20px 5px 20px;
            background-color: rgb(29, 144, 245);
            color: #fff;
            &:hover {
              background-color: rgb(26, 129, 219);
            }
          }
          .chat-img {
            img {
              max-width: 300px;
              max-height: 200px;
              border-radius: 10px;
            }
          }
          .info-time {
            margin: 10px 0;
            color: #fff;
            font-size: 14px;
            display: flex;
            justify-content: flex-end;

            img {
              width: 30px;
              height: 30px;
              border-radius: 50%;
              vertical-align: middle;
              margin-left: 10px;
            }
            span {
              line-height: 30px;
            }
            span:first-child {
              color: rgb(101, 104, 115);
              margin-right: 10px;
              vertical-align: middle;
            }
          }
        }
      }
    }
    .chatInputs {
      width: 90%;
      position: absolute;
      bottom: 0;
      margin: 3%;
      display: flex;
      .boxinput {
        width: 50px;
        height: 50px;
        background-color: rgb(66, 70, 86);
        border-radius: 15px;
        border: 1px solid rgb(80, 85, 103);
        position: relative;
        cursor: pointer;
        img {
          width: 30px;
          height: 30px;
          position: absolute;
          left: 50%;
          top: 50%;
          transform: translate(-50%, -50%);
        }
      }
      .emoji {
        transition: 0.3s;
        &:hover {
          background-color: rgb(46, 49, 61);
          border: 1px solid rgb(71, 73, 82);
        }
      }

      .inputs {
        width: 90%;
        height: 50px;
        background-color: rgb(66, 70, 86);
        border-radius: 15px;
        border: 2px solid rgb(34, 135, 225);
        padding: 10px;
        box-sizing: border-box;
        transition: 0.2s;
        font-size: 20px;
        color: #fff;
        font-weight: 100;
        margin: 0 20px;
        &:focus {
          outline: none;
        }
      }
      .send {
        background-color: rgb(29, 144, 245);
        border: 0;
        transition: 0.3s;
        box-shadow: 0px 0px 5px 0px rgba(0, 136, 255);
        &:hover {
          box-shadow: 0px 0px 10px 0px rgba(0, 136, 255);
        }
      }
    }
  }
}
</style>
